package org.jcms.web.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcms.model.Admin;

/**
 * admin目录权限过滤
 * 
 * @author 赵泽洋
 * 
 */
@WebFilter("/EncodedFilter")
public class AdminFilter implements Filter {

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		Admin admin = (Admin) req.getSession().getAttribute("admin");
		
		if (admin != null) {
			chain.doFilter(request, response);
		} else {
			String ctp = req.getSession().getServletContext().getContextPath();
			res.sendRedirect(ctp + "/illegal.jsp");
			return;
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void destroy() {

	}

}
